// Generated using webpack-cli https://github.com/webpack/webpack-cli

const path = require("path");
const TerserPlugin = require("terser-webpack-plugin");
const { merge } = require("lodash");

const isProduction = process.env.NODE_ENV == "production";

const config = {
  entry: { index: "./src/index.ts" },
  output: {
    filename: (pathData, _) => {
      return `${pathData.chunk.name}.js`;
    },
    path: path.resolve(__dirname, "dist"),
  },
  plugins: [
    // Add your plugins here
    // Learn more about plugins from https://webpack.js.org/configuration/plugins/
  ],
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/i,
        loader: "ts-loader",
        exclude: ["/node_modules/"],
        options: {
          configFile: path.resolve(__dirname, "tsconfig.json"),
        },
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
        type: "asset",
      },

      // Add your rules for custom modules here
      // Learn more about loaders from https://webpack.js.org/loaders/
    ],
  },
  resolve: {
    extensions: [".tsx", ".ts", ".jsx", ".js", "..."],
  },
};

module.exports = () => {
  if (isProduction) {
    return merge(config, {
      mode: "production",
      optimization: {
        minimize: true,
        minimizer: [
          new TerserPlugin({
            terserOptions: {
              // 尝试对eval内容进行压缩
              mangle: {
                eval: true,
              },
              format: {
                // 去除空格相关设置
                beautify: false,
                comments: false,
                indent_level: 0,
                preserve_line: false,
              },
            },
          }),
        ],
      },
    });
  } else {
    return merge(config, {
      mode: "development",
    });
  }
};
